package java_DesignPattern.StructuralPattern.CompositePattern;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单类
 */
public class Menu extends MenuCompoment{

    //内聚MenuCompoment
    //声明组件集合,因为子菜单与子菜单项都是菜单组件的子类
    private List<MenuCompoment> menuCompomentList = new ArrayList<>();

    /**
     * 构建菜单
     * @param name
     * @param level
     */
    public Menu(String name, int level) {
        super(name ,level);
    }

    @Override
    public void add(MenuCompoment menuCompoment) {
        menuCompomentList.add(menuCompoment);
    }

    @Override
    public void remove(MenuCompoment menuCompoment) {
        menuCompomentList.remove(menuCompoment);
    }

    @Override
    public void print() {
        for (int i = 0; i < level; i++) {
            System.out.print("-");
        }

        //打印当前菜单的名称
        System.out.println(getName());

        //打印子菜单及其子菜单项的名称
        for (MenuCompoment menuCompoment : menuCompomentList) {
            menuCompoment.print();
        }
    }

    @Override
    public MenuCompoment getIndex(int i) {
        return menuCompomentList.get(i);
    }

    @Override
    public String getName() {
        return super.getName();
    }
}
